package com.xhs.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;

/**
 * @desc: CompletableFuture使用案例
 * @projectName: java-tools-parent
 * @author: xhs
 * @date: 2025-08-09 周六 14:47
 */
@Slf4j
@RestController
@RequestMapping("/completableFuture")
public class CompletableFutureController {

    @Resource
    private Executor taskExecutor;

    /**
     * 没有返回值，系统自带线程池
     */
    @GetMapping("/runAsync")
    public void runAsync() {
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.runAsync,没有返回值，系统自带线程池 开始:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
        CompletableFuture.runAsync(() -> {
            log.info("\n 线程：{}", Thread.currentThread().getName());
            log.info("\n hello world");
        });
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.runAsync,没有返回值，系统自带线程池 结束:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
    }

    /**
     * 没有返回值，指定线程池
     */
    @GetMapping("/runAsyncWithExecutor")
    public void runAsyncWithExecutor() {
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.runAsync,没有返回值，指定线程池 开始:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
        CompletableFuture.runAsync(() -> {
            log.info("\n 线程：{}", Thread.currentThread().getName());
            log.info("\n hello world");
        }, taskExecutor);
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.runAsync,没有返回值，指定线程池 结束:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
    }

    /**
     * 有返回值，系统自带线程池
     */
    @GetMapping("/supplyAsync")
    public void supplyAsync() throws ExecutionException, InterruptedException {
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.supplyAsync,有返回值，系统自带线程池 开始:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            log.info("\n 线程：{}", Thread.currentThread().getName());
            log.info("\n hello world");
            return "hello world";
        });
        log.info("\n 执行结果：{}", future.get());
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.supplyAsync,有返回值，系统自带线程池 结束:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
    }

    /**
     * 有返回值，指定线程池
     */
    @GetMapping("/supplyAsyncWithExecutor")
    public void supplyAsyncWithExecutor() throws ExecutionException, InterruptedException {
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.supplyAsync,有返回值，指定线程池 开始:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            log.info("\n 线程：{}", Thread.currentThread().getName());
            log.info("\n hello world");
            return "hello world";
        }, taskExecutor);
        log.info("\n 执行结果：{}", future.get());
        log.info("\n >>>>>>>>>>>>>>CompletableFuture.supplyAsync,有返回值，指定线程池 结束:{}<<<<<<<<<<<<<<<<<<<<", Thread.currentThread().getName());
    }
}
